home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / DELPHI32 / GRAPHICS / TS32 / POLYGONS.PAS < prev    next >
Pascal/Delphi Source File  |  1996-02-12  |  5KB  |  204 lines

  1. unit PolygonSprite;
  2.  
  3. (*********************************************
  4. TPolygonSprite->TSprite
  5.  
  6. This descendant of TSprite renders itself by drawing
  7. a set of lines.  This is an abstract class ... to
  8. implement, derive a new class and override the
  9. AddVertices method.  The the sample TSquareSprite that
  10. is provided at the end of this unit as an example.
  11.  
  12. Properties
  13.  
  14. Angle-
  15.   Controls the angle of rotation of the polygon.
  16. ColorIndex-
  17.   The color of the polygon's lines.
  18. Spin-
  19.   The direction of the polygon's spin, if any.
  20. SpinSpeed-
  21.   The speed at which the polygon spins.  The higher the
  22.   number, the faster the spin.
  23.  
  24. Events
  25.  
  26. Methods
  27.  
  28. AddVertices-
  29.   This method MUST be overriden in derived classes.
  30.   It's purpose is to populate the Vertex list of
  31.   the TPolygon object contained within this class.
  32.   See the TSquareSprite at the end of this unit as
  33.   an example.
  34. CreatePolygonSprite-
  35.   Pass the colorindex of the sprite as a constructor
  36.   paramater.
  37. *********************************************)
  38.  
  39. interface
  40.  
  41. uses
  42.   Windows, SysUtils, Classes, Graphics, Controls, DIBDrawingSurface, Sprite,
  43.   Grafix;
  44.  
  45. type
  46.  
  47.   TRotation = ( rotNone, rotClockwise, rotCounterClockwise );
  48.  
  49.   TPolygonSprite = class( TSprite )
  50.   private
  51.      r: TRectangle;
  52.      FSpin: TRotation;
  53.      FSpinSpeed: integer;
  54.   protected
  55.      poly: TPolygon;
  56.      polyOriginal: TPolygon;
  57.      FAngle: integer;
  58.      FColorIndex: byte;
  59.      procedure SetAngle( n: integer );
  60.      procedure SetSize; virtual;
  61.   public
  62.      constructor CreatePolygonSprite( const nCol: byte );
  63.      destructor Destroy; override;
  64.      procedure AddVertices; virtual; abstract;
  65.      procedure Render; override;
  66.      property Angle: integer read FAngle write SetAngle;
  67.      property ColorIndex: byte read FColorIndex write FColorIndex;
  68.      property Spin: TRotation read FSpin write FSpin;
  69.      property SpinSpeed: integer read FSpinSpeed write FSpinSpeed;
  70.   end;
  71.  
  72.   TSquareSprite = class( TPolygonSprite )
  73.   private
  74.   protected
  75.      procedure AddVertices; override;
  76.   public
  77.   end;
  78.  
  79. implementation
  80.  
  81. constructor TPolygonSprite.CreatePolygonSprite( const nCol: byte );
  82. begin
  83.   inherited Create;
  84.  
  85.   Spin := rotNone;
  86.   SpinSpeed := 10;
  87.   Angle := 0;
  88.   FColorIndex := nCol;
  89.   poly := TPolygon.Create;
  90.  
  91.   AddVertices;
  92.  
  93.   r := TRectangle.CreateRectangle( 0, 0, 0, 0 );
  94.  
  95.   SetSize;
  96.  
  97.   polyOriginal := TPolygon.Create;
  98.   polyOriginal.Assign( poly );
  99. end;
  100.  
  101. destructor TPolygonSprite.Destroy;
  102. var
  103.   i: integer;
  104. begin
  105.   polyOriginal.Free;
  106.   poly.Free;
  107.   r.Free;
  108.   inherited Destroy;
  109. end;
  110.  
  111. (*********************************************
  112. Spinning of the polygon is not considered important,
  113. so it is done within render so it will not take
  114. up processing time if the sprite is off the edge
  115. of the surface.
  116. *********************************************)
  117. procedure TPolygonSprite.Render;
  118. var
  119.   i: integer;
  120.   ptFrom, ptTo: TPoint;
  121.   OldAngle: integer;
  122. begin
  123.   inherited Render;
  124.  
  125.   OldAngle := Angle;
  126.   case Spin of
  127.      rotCounterClockwise:
  128.         Angle := Angle + SpinSpeed;
  129.      rotClockwise:
  130.         Angle := Angle - SpinSpeed;
  131.   end;
  132.  
  133.   if Angle <> OldAngle then
  134.      begin
  135.         poly.Assign( polyOriginal );
  136.         poly.Rotate( TDegree( Angle ) );
  137.         SetSize;
  138.      end;
  139.  
  140.   ptFrom := poly.Vertex[0].Point;
  141.  
  142.   Inc( ptFrom.X, ptPhysical.X );
  143.   Inc( ptFrom.Y, ptPhysical.Y );
  144.  
  145.   dds.DIBCanvas.MoveTo( ptFrom.X, ptFrom.Y );
  146.   dds.DIBCanvas.PenColorIndex := FColorIndex;
  147.  
  148.   for i := 1 to poly.VertexCount - 1 do
  149.      begin
  150.         ptTo := poly.Vertex[i].Point;
  151.         Inc( ptTo.X, ptPhysical.X );
  152.         Inc( ptTo.Y, ptPhysical.Y );
  153.         dds.DIBCanvas.LineTo( ptTo.X, ptTo.Y );
  154.      end;
  155.  
  156.   dds.DIBCanvas.LineTo( ptFrom.X, ptFrom.Y );
  157. end;
  158.  
  159. procedure TPolygonSprite.SetAngle( n: integer );
  160. begin
  161.   FAngle := IntToDegree( n );
  162. end;
  163.  
  164. procedure TPolygonSprite.SetSize;
  165. var
  166.   i: integer;
  167. begin
  168.   r.Left := 0;
  169.   r.Right := 0;
  170.   r.Top := 0;
  171.   r.Bottom := 0;
  172.   for i := 0 to poly.VertexCount - 1 do
  173.      with poly.Vertex[i] do
  174.         begin
  175.            if X < r.Left then
  176.               r.Left := X;
  177.            if Y < r.Top then
  178.               r.Top := Y;
  179.            if X > r.Right then
  180.               r.Right := X;
  181.            if Y > r.Bottom then
  182.               r.Bottom := Y;
  183.         end;
  184.   Width := r.Width + 2;
  185.   Height := r.Height + 2;
  186. end;
  187.  
  188. (*********************************************
  189. TSquareSprite
  190. An example descendant of TPolygonSprite.
  191. *********************************************)
  192. procedure TSquareSprite.AddVertices;
  193. begin
  194.   with poly do
  195.      begin
  196.         AddVertex( TVertex.CreateVertex( -5, -5 ) );
  197.         AddVertex( TVertex.CreateVertex( 5, -5 ) );
  198.         AddVertex( TVertex.CreateVertex( 5, 5 ) );
  199.         AddVertex( TVertex.CreateVertex( -5, 5 ) );
  200.      end;
  201. end;
  202.  
  203. end.
  204.